/*
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/util.h>

// http://123.58.175.113:8084/cgi-bin/bot.cgi/
//

// http://123.125.48.83:7660/urs/regmobile

static void dump_evbuffer(struct evbuffer *buf)
{
	int len = evbuffer_get_length(buf);
	char *temp = new char[len];
	evbuffer_copyout(buf, temp, len);
	printf("response: \n%s\n", temp);
	delete []temp;
}

void req_cb(struct evhttp_request *req, void *ctx)
{
	struct evhttp_connection *conn = (evhttp_connection*)ctx;

	struct bufferevent *bev = evhttp_connection_get_bufferevent(conn);
	dump_evbuffer(bufferevent_get_input(bev));

	if (req == NULL) {
		printf("time out");
	} else {
		printf("code:%d, text = %s\n", req->response_code, req->response_code_line);

		struct evkeyvalq *head_kv = evhttp_request_get_input_headers(req);

		printf("Transfer-Encoding: %s\n", evhttp_find_header(head_kv, "Transfer-Encoding"));
		printf("Content_Length: %s\n", evhttp_find_header(head_kv, "Content_Length"));

		struct evbuffer *buf = evhttp_request_get_input_buffer(req);
		dump_evbuffer(buf);
	}
}

static void chunked_cb(struct evhttp_request *req, void *ctx)
{
	printf("chunked ............\n");
	if (req == NULL) {
		printf("time out");
	} else {
		printf("code:%d, text = %s\n", req->response_code, req->response_code_line);

		struct evkeyvalq *head_kv = evhttp_request_get_input_headers(req);

		printf("Transfer-Encoding: %s\n", evhttp_find_header(head_kv, "Transfer-Encoding"));
		printf("Content_Length: %s\n", evhttp_find_header(head_kv, "Content_Length"));
		struct evbuffer *buf = evhttp_request_get_input_buffer(req);
		int len = evbuffer_get_length(buf);
		char *temp = new char[len];
		evbuffer_remove(buf, temp, len);
		printf("response: \n%s\n", temp);
		delete []temp;
	}
}

static void close_cb(struct evhttp_connection *conn, void *ctx)
{
	printf(" --- conn get close --- \n");
}

int main(int argc, char **argv)
{
	/*
	const char *host = "192.168.11.218";
	const char *query = "/";
	int port = 80;
	*/

	/*
	const char *host = "123.58.175.113";
	const char *query = "/cgi-bin/bot.cgi/";
	int port = 8084;
	*/

	// http://123.125.48.83:7660/urs/regmobile
	
	const char *host = "123.125.48.83";
	const char *query = "/urs/regmobile";
	int port = 7660;


	struct event_base *base = event_base_new();
	struct evhttp_connection *conn = evhttp_connection_base_new(base, NULL/* struct evdns_base *dnsbase */, host, port);
	if (!conn) {
		perror(" new evhttp_request failed.\n");
	}
	evhttp_connection_set_closecb(conn, close_cb, NULL);
	struct evhttp_request *req = evhttp_request_new(req_cb, (void*)conn);
	// evhttp_request_set_chunked_cb(req, chunked_cb);
	evhttp_add_header(req->output_headers, "Host", host);
	// evhttp_add_header(req->output_headers, "Content-Length", "0");
	evhttp_add_header(req->output_headers, "Connection", "Close");

	evhttp_make_request(conn, req, EVHTTP_REQ_GET, query);
	evhttp_connection_set_timeout(conn, 5);

	event_base_dispatch(base);
	// evhttp_request_free(req);
	evhttp_connection_free(conn);
	event_base_free(base);
	// evhttp_uri_free(uri);
}


